/*********************************************************
 * copyright xinbohit.com 版权所有 
 * 开发人员：IvanYu
 * 创建时间：2014/8/27 20:37:41
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LEARNREN.COM.Entity;

namespace LEARNREN.COM.BLL
{
    public class AllCatalogueNC
    {
        private Dictionary<long, string> dicNC = null;
        public AllCatalogueNC()
        {
            dicNC = new Dictionary<long, string>();
            Reload();
        }

        public string this[long cid]
        {
            get
            {
                return dicNC[cid];
            }
        }

        public void Reload()
        {
            List<Catalogue> cl = BasicCache.AllCatalogueList;
            dicNC.Clear();
            foreach (var c in cl)
            {
                dicNC.Add(c.Id, GetPath(c.Id));
            }

            string shouye;
            string format = "<span class=\"nslink\"><a href=\"{0}\" title=\"{1}\" class=\"nslinka\">{2}</a></span>";
            SiteConfig siteConfig = BasicCache.SiteConfig;
            if (siteConfig.IsStaticEnable)
            {
                shouye = string.Format(format, System.Web.VirtualPathUtility.ToAbsolute("~/default.html"), siteConfig.SiteName, siteConfig.SiteName);
            }
            else
            {
                shouye = string.Format(format, System.Web.VirtualPathUtility.ToAbsolute("~/default.aspx"), siteConfig.SiteName, siteConfig.SiteName);
            }
            dicNC.Add(0, shouye);
        }

        private string GetPath(long cid)
        {
            string format = "<span class=\"nslink\"><a href=\"{0}\" title=\"{1}\" class=\"nslinka\">{2}</a></span>";
            SiteConfig siteConfig = BasicCache.SiteConfig;
            List<Catalogue> cl = BasicCache.AllCatalogueList;

            string result = string.Empty;

            while (cid != 0)
            {
                Catalogue c = cl.Where(p => p.Id == cid).FirstOrDefault();
                if (c != null)
                {
                    if (siteConfig.IsStaticEnable)
                    {
                        result = "<span class=\"nspliter\"> &gt </span>" + string.Format(format, c.HtmlPath, c.Title, c.Title) + result;
                    }
                    else
                    {
                        result = "<span class=\"nspliter\"> &gt </span>" + string.Format(format, System.Web.VirtualPathUtility.ToAbsolute(string.Format("~/{0}.aspx?id={1}", c.CatalogueType, c.Id)),
                                               c.Title,
                                               c.Title) + result;
                    }
                    cid = c.ParentId;
                }
                else
                {
                    break;
                }
            }

            if (siteConfig.IsStaticEnable)
            {
                result = string.Format(format, System.Web.VirtualPathUtility.ToAbsolute("~/default.html"), siteConfig.SiteName, siteConfig.SiteName) + result;
            }
            else
            {
                result = string.Format(format, System.Web.VirtualPathUtility.ToAbsolute("~/default.aspx"), siteConfig.SiteName, siteConfig.SiteName) + result;
            }

            return result;
        }
    }
}
